var map;
$(function () {
    map = new Map();
    $("#list").datagrid({
        method: "get",
        url: getRootPath() + "/sop/getSopRecordDownloadList",
        fit: true,
        border: true,
        pageNumber: 1,
        pageSize: 20,
        pageList: [10, 20, 30, 50, 100],
        columns: [[
            {field:'ck',checkbox:true},
            {field: "FBOMNumber", title: "产成品BOM编码", width: 180},
            {field: "FModel", title: "产成品规格型号", width: 160},
            {field: "modelName",title:"机型",width: 160,
                formatter:function(value,row,index){
                   /* var modelName = "";
                    var fBOMNumber = row.FBOMNumber;
                    var l = fBOMNumber.length;
                    modelName = fBOMNumber.substring(6,l-2);
                    console.log("fBOMNumber:"+fBOMNumber);
                    console.info("formatter执行了");*/
                    var pattern = /[A-Z,0-9]{6,}/,
                        str = row.FModel;
                    var modelName = pattern.exec(str);
                    if (modelName.length>0)
                    {
                        console.info(modelName[0]);
                        map.set(row.FBOMNumber,modelName[0]);
                        return modelName[0];
                    }
                    else
                    {return "此项机型获取失败请手动查询";}

                }
            },
            // {field: "FCreateName", title: "BOM创建者", width: 60},
            {field: "FNumber", title: "半成品编码", width: 160},
            {field: "FName", title: "物料名称", width: 60},
            {field: "FModel1", title: "半成品规格型号", width: 180},
            {field: "bom_module_num", title: "SOP所属BOM模块短编码", width: 160},
            {field: "version", title: "SOP版本(最新)", width: 50},
            {field: "controlled_num", title: "SOP受控编码", width: 100},
            {field: "date", title: "SOP上传日期", width: 100},
            {field: "operator", title: "操作人", width: 60},
            {field: "production_process", title: "工段", width: 80},
            // {field: "remark", title: "备注", width: 30},
            {field:"file",title:"附件操作",width:250,
                formatter:function(value,row,index){
                if (row.download_url!=null)
                {
                    var elementNode ="<span>"
                    var f = "<a href='"+window.location.protocol + "//" + window.location.host + "/downloadSOP"+row.download_url+"' class='button-edit button-blue'>下载</a>";
                    var btn = "<button style=\"margin-left: 5px;\" onclick=\"openpreview('"+window.location.protocol+"//"+window.location.host+"/downloadSOP"+row.download_url+"')\" class=\"button-edit button-blue\">在线预览</button>";
                    elementNode=elementNode+f+btn+"</span>"
                    return elementNode;
                }
                else
                {return "SOP附件不存在";}

                }
            },
        ]],
        groupField:"FBOMNumber",
        view:groupview,
        groupFormatter:function(value, rows){
            return value + ' - ' + rows.length + ' 项子模块';
        },
        onLoadSuccess: function (data) {
            $('.button-delete').linkbutton({});
            $('.button-edit').linkbutton({});
            $('#list').datagrid("selectRow", 0);
            var row = $("#list").datagrid('getSelected');
            console.log(row.FBOMNumber+"------")
            setVal("#modelName", map.get(row.FBOMNumber));
            queryModelList();
        },
        pagination: true,
        striped: true,
        singleSelect:true,
        //工具条
        toolbar: "#toolbar",
        onDblClickRow: function(index,row){
            setVal("#modelName", map.get(row.FBOMNumber));
            queryModelList();
        }
    });
    $("#model_list").datagrid({
        method: "get",
        url: getRootPath() + "/sop/getModelRecordDownloadList",
        fit: true,
        border: true,
        pageNumber: 1,
        pageSize: 20,
        pageList: [10, 20, 30, 50, 100],
        columns: [[
            {field:'ck',checkbox:true},
            {field: "machine_model",title:"机型",width: 160},
            // {field: "FCreateName", title: "BOM创建者", width: 60},
            {field: "version", title: "版本", width: 50},
            {field: "controlled_num", title: "受控编码", width: 100},
            {field: "date", title: "上传日期", width: 100},
            {field: "operator", title: "操作人", width: 60},
            {field: "production_process", title: "工段", width: 80},
            // {field: "remark", title: "备注", width: 30},
            {field:"file",title:"附件操作",width:250,
                formatter:function(value,row,index){
                    if (row.download_url!=null)
                    {
                        var elementNode ="<span>"
                        var f = "<a href='"+window.location.protocol + "//" + window.location.host + "/downloadMtSOP"+row.download_url+"' class='button-edit button-blue'>下载</a>";
                        var btn = "<button style=\"margin-left: 5px;\" onclick=\"openpreview('"+window.location.protocol+"//"+window.location.host+"/downloadMtSOP"+row.download_url+"')\" class=\"button-edit button-blue\">在线预览</button>";
                        elementNode=elementNode+f+btn+"</span>"
                        return elementNode;
                    }
                    else
                    {return "SOP附件不存在";}

                }
            },
        ]],
        groupFormatter:function(value, rows){
            return value + ' - ' + rows.length + ' 项子模块';
        },
        onLoadSuccess: function (data) {
            $('.button-delete').linkbutton({});
            $('.button-edit').linkbutton({});
        },
        pagination: true,
        striped: true,
        singleSelect:true
    });

    $("#ExportAllBomData").click(function() {
        window.location.href=getRootPath() +'/sop/exportSOPRelationshipData';
    });


    //通过K3编码找到对应信息
    // $("#typeDtlNo").textbox('textbox').keydown(function (e) {
    //     if (e.keyCode == 13) {
    //         var value = $("input[name='typeDtlNo']").val().trim().toUpperCase();
    //         $.getJSON(getRootPath() + "/mtManage/assets/mtInfo?typeDtlNo=" + value, function (json) {
    //             if (json.code == 200) {
    //                 $("#editForm").form("load", json.data);
    //                 $("#stockin_user").textbox({
    //                     value:username
    //                 });
    //                 $.messager.show({
    //                     title: "提示",
    //                     msg: "查询成功"
    //                 });
    //             } else {
    //                 $.messager.alert("提示", "未查询到对应数据:" + json.message, "error");
    //                 $("#editForm").form("clear");
    //             }
    //         })
    //     }
    // });


    //保存商品
    // $("#save").click(function () {
    //     var formUrl;
    //     if($("#record_id").val()==""){
    //         formUrl=getRootPath() + "/mtManage/assets/addMtRecord";
    //     }else{
    //         formUrl=getRootPath() + "/mtManage/assets/updateMtRecord";
    //     }
    //     $("#editForm").form("submit", {
    //         url:formUrl ,
    //         onSubmit: function () {
    //             //表单验证
    //             return $("#editForm").form("validate");
    //         },
    //         success: function (data) {
    //             // $.messager.alert("提示", "保存失败:" + data, "error");
    //             data = eval("(" + data + ")");//转成json
    //             if (data.message=='ok') {
    //                 //1.表单清空
    //                 $("#editForm").form("clear");
    //                 //2.关闭窗口
    //                 $("#editWin").window("close");
    //                 //3.刷新datagrid
    //                 $("#list").datagrid("reload");
    //                 if(formUrl==getRootPath() + "/mtManage/assets/updateMtRecord"){
    //                     $.messager.show({
    //                         title: "提示",
    //                         msg: "修改成功"
    //                     });
    //                 }else{
    //                     $.messager.show({
    //                         title: "提示",
    //                         msg: "新增成功"
    //                     });
    //                 }
    //             } else {
    //                 if(formUrl==getRootPath() + "/mtManage/assets/updateMtRecord"){
    //                     $.messager.alert("提示", "修改失败:" + data.message, "error");
    //                 }else{
    //                     $.messager.alert("提示", "新增失败:" + data.message, "error");
    //                 }
    //             }
    //         }
    //     });
    // });
    //
    // $("#reset").click(function () {
    //     $("#editForm").form("clear");
    // });

    //修改商品 -- 回显商品信息
    // $("#editBtn").click(function () {
    //
    //     //1.获取选择的商品
    //     var rows = $("#list").datagrid("getSelections");
    //
    //     //判断一次只能选择一个
    //     if (rows.length != 1) {
    //         $.messager.alert("提示", "一次只能选择一行", "warning");
    //         return;
    //     }
    //
    //     //2.获取第一行
    //     var row = rows[0];
    //
    //     //3.到后台商品数据，填充到表单
    //     $.getJSON(getRootPath() + "/mtManage/assets/recordInfo?id=" + row.id, function (json) {
    //         $("#editForm").form("load", json.data);
    //         console.log($("#record_id").val()+"==id");
    //     })
    //
    //     //4.弹出编辑窗口
    //     $("#editWin").window("open");
    // });

    //删除商品
    // $("#deleteBtn").click(function () {
    //     //1.获取选择的商品
    //     var rows = $("#list").datagrid("getSelections");
    //
    //     if (rows.length == 0) {
    //         $.messager.alert("提示", "至少选择一条记录", "warning");
    //         return;
    //     }
    //
    //     //2.获取记录的id 格式： 字符串  1,2,3
    //     var ids = new Array();
    //     $(rows).each(function (i) {
    //         ids.push(rows[i].id);
    //     });
    //
    //     ids = ids.join(",");//用逗号进行分隔
    //     //通过提示框确认是否进行删除
    //     $.messager.confirm('提示', '确定要删除吗？', function(r){
    //         if (r){
    //             //3.发送id到后台进行删除
    //             $.post(getRootPath() + "/sop/deleRecordInfo", {"ids": ids}, function (data) {
    //                 if (data.message=="ok") {
    //                     //刷新datagrid
    //                     $("#list").datagrid("reload");
    //                     //提示
    //                     $.messager.show({
    //                         title: "提示",
    //                         msg: "删除成功"
    //                     });
    //                 } else {
    //                     $.messager.show({
    //                         title: "提示",
    //                         msg: "删除失败"
    //                     });
    //                 }
    //             }, "json");
    //         }
    //     });
    // });
});

function queryModelList() {
    var modelName = $("#modelName").val().trim();
    $("#model_list").datagrid('load', {
            "modelName": modelName
        }
    );
}

function openpreview(file)
{
    console.info(file);
    var url = file;
    window.open('http://192.168.202.201:8012/onlinePreview?url='+encodeURIComponent(Base64.encode(url)),
        "在线预览",
        "height=768,width=1024,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=no,status=no"
        );
}

//单元格扩展悬浮显示功能
var columShow = function colum_Show(value,row,index){
    return '<span title='+value+'>'+value+'</span>'
}

// 记录查询
function queryRecord() {
    //console.log($("#stockin_man_s").val());
    $("#list").datagrid('load', {
            bomMoudleNum:$("#bom_moudle_num_s").val(),
            queryType:$("input[name='queryType']:checked").val()
            //operator:$("#operator_man_s").val()
        }
    );
}